﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_IncomeOnMonth : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IPayrollsBLL prBLL = new PayrollsBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();

        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
 
            txtSalary.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "PAY_DTLS_SEQ") == null ? null : DataBinder.Eval(DataItem, "PAY_DTLS_SEQ").ToString();
                    cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FULL_NAME").ToString(), DataBinder.Eval(DataItem, "PD_EMP_SEQ").ToString()));
                    cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "PD_EMP_SEQ").ToString();
                    txtSalary.Text = DataBinder.Eval(DataItem, "PD_SALARY") == null ? null : DataBinder.Eval(DataItem, "PD_SALARY").ToString();
                    txtRemark.Text = DataBinder.Eval(DataItem, "PD_REMARKS") == null ? null : DataBinder.Eval(DataItem, "PD_REMARKS").ToString();
                    cmbEmployee.Enabled = false;
                }
                else
                {
                    cmbEmployee.Enabled = true;
                }
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbEmployee.Items.Clear();

            DataSet ds = empBLL.LoadEmployees((int?)null, "1", true, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["FULL_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(150);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["POS_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(100);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdPosRef.Width = Unit.Pixel(100);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                //((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                ((Table)cmbEmployee.Items[j].FindControl("tableTmpEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                int payRoll_id = 0;
                string str = Session["et|m|y"].ToString();
                string[] arr = str.Split('|');
                DataSet ds = new DataSet();

                bool dup = prBLL.ChkDuplicatePayrolls(int.Parse(arr[0]), int.Parse(arr[2]), int.Parse(arr[1]));
                if (dup != true)
                {
                    prBLL.InsertPayrolls(int.Parse(arr[0]), int.Parse(arr[2]), int.Parse(arr[1]), ssMng.EmpSeq);
                }

                ds = prBLL.ListPayrolls(int.Parse(arr[0]), int.Parse(arr[2]), int.Parse(arr[1]));
                payRoll_id = int.Parse(ds.Tables[0].Rows[0]["pay_seq"].ToString());

                int ID_emp = Convert.ToInt32(cmbEmployee.SelectedValue);
                bool dubDtls = prBLL.ChkDuplicatePayrollsDtls(payRoll_id, ID_emp);
                //bool dubDtls = prBLL.ChkDuplicatePayrollsDtls(payRoll_id, int.Parse(lblID_emp.Text));

                if (dubDtls != true)
                {
                    //prBLL.Insert(payRoll_id, int.Parse(lblID_emp.Text), Double.Parse(txtSalary.Text), txtRemark.Text, ssMng.EmpSeq);
                    prBLL.Insert(payRoll_id, ID_emp, Double.Parse(txtSalary.Text), txtRemark.Text, ssMng.EmpSeq);
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                prBLL.Update(int.Parse(lblID.Text), Double.Parse(txtSalary.Text), txtRemark.Text, ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }


        
    }
}